<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="群名称" prop="name">
				<el-input v-model="form.name" placeholder="群名称" maxlength="48" clearable></el-input>
			</el-form-item>
			<el-form-item label="群简介">
				<el-input type="textarea" :rows="2" maxlength="180" placeholder="群简介" v-model="form.introduce"></el-input>
			</el-form-item>
			<el-form-item label="是否启用" prop="isDeleted">
				<el-switch v-model="form.isDeleted" :active-value="0" :inactive-value="1"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	components: {
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '创建群对话框',
				edit: '编辑群对话框'
			},
			form: {
				id: "",
				name: "",
				isDeleted:0,
				introduce:"",
			},
			rules: {
				name: [
					{required: true, message: '请填写标题'}
				],
			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					var res;
					if(this.mode=="add")
					{
						this.isSaveing = true;
						res = await this.$API.chat.group.addChatGroup.post(this.form);
						this.isSaveing = false;
					}else if(this.mode=="edit")
					{
						this.isSaveing = true;
						res = await this.$API.chat.group.updateChatGroup.post(this.form);
						this.isSaveing = false;
					}
					if (res.code == 200) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
			this.form.name = data.name
			this.form.isDeleted = data.isDeleted
			this.form.introduce = data.introduce
		}
	}
}
</script>

<style>
</style>
